﻿#include "mini_gui.h"
#include "wnd_main.h"

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/ctrl/static.h>
#include <minigui/ctrl/button.h>
#include <minigui/ctrl/edit.h>
#include <minigui/ctrl/scrollview.h>
#include <minigui/ctrl/scrollbar.h>
#include <mgi/mgi.h>


#include "wnd_handle.h"
#include "wnd_api.h"

#if 1
//UI_MENU_S *pstGlbMenu;


static int ARTANIS_ENVIRONMENT_Init(void *pArg)
{
	//char s8ClientName[64];
	pstGlbRedisProc = REDIS_PROC_Create((char *)UI_MSG_LIST_KEY, MC_REDIS_MSG_Handle);
	if(NULL == pstGlbRedisProc) {
		SLOG(ERR, "REDIS_PROC_Create failed\n");
		return -1;
	}

	REDIS_PROC_SetConnectCallBack(pstGlbRedisProc, MC_REDIS_ConnectCallBack);
	REDIS_PROC_Start(pstGlbRedisProc);

/*

ARTANIS_ENVIRONMENT_Init_Test:
	//测试是否连接正常
	memset(s8ClientName, 0, sizeof(s8ClientName));
	REDIS_ClientGetName(pstGlbRedisProc->pstRedisHandle, s8ClientName, sizeof(s8ClientName));
	if(pstGlbRedisProc->u32RunSta == 0 || memcmp(s8ClientName, UI_CLILENT_NAME, sizeof(UI_CLILENT_NAME)) != 0) {
		//s32Count++;
		//if(s32Count >= 10*20) {
		//	SYS_API_Reboot();
		//}
		usleep(300000);
		//SLOG(DBG, "连接中...[%u] [%s]\n", pstGlbRedisProc->u32RunSta, s8ClientName);
		goto ARTANIS_ENVIRONMENT_Init_Test;
	}
*/

	//bRunFlag = true;

	return 0;
}


static void * ARTANIS_ENVIRONMENT_Proc(void *pArg)
{
	return 0;
}


static int ARTANIS_ENVIRONMENT_Deinit(void *pArg)
{
	//bRunFlag = false;
	REDIS_PROC_Destroy(pstGlbRedisProc);
	pstGlbRedisProc = NULL;
	return 0;
}


static BOOL g_ime_opened2 = FALSE;
//unsigned int u32GlbSoftKeypadWnd;

void notify_ime_status2(BOOL opened)
{
    g_ime_opened2 = opened;
}



int ARTANIS_WINDOW_Init(void *pArg)
{

	QLOG(TDBG, "创建键盘\n");

	//mgiAddIMEWindow (ime_handle, u32SoftKeypadWnd, "softkeyboard");
	stGlbMainWndHandle.u32SoftKeyWndId = mgiCreateSoftKeypad(notify_ime_status2);
	//SetIMEStatus(IME_STATUS_AUTOTRACK, TRUE);
	SetIMEStatus(IME_STATUS_ENCODING, IME_ENCODING_UTF8);
	SetIMEStatus(IME_STATUS_ENABLED, TRUE);

	// 获取屏幕宽度
#if 1
	stGlbMainWndHandle.s32ScreenWidth = GetGDCapability(HDC_SCREEN,GDCAP_HPIXEL) - 12;
	stGlbMainWndHandle.s32ScreenHeight = GetGDCapability(HDC_SCREEN,GDCAP_VPIXEL) - 3;


	//stGlbMainWndHandle.s32RealX = 50;
	//stGlbMainWndHandle.s32RealY = 0;
	//stGlbMainWndHandle.s32RealWidth = stGlbMainWndHandle.s32ScreenWidth - stGlbMainWndHandle.s32RealX;
	//stGlbMainWndHandle.s32RealHeight = stGlbMainWndHandle.s32ScreenHeight - stGlbMainWndHandle.s32RealY;
#else
	stGlbMainWndHandle.s32ScreenWidth = GetGDCapability(HDC_SCREEN,GDCAP_HPIXEL);
	stGlbMainWndHandle.s32ScreenHeight = GetGDCapability(HDC_SCREEN,GDCAP_VPIXEL);
#endif
	WND_WIDGET_InitFont(&stGlbMainWndHandle.stDefaultFont);

	QLOG(DBG, "获取到屏幕尺寸 宽[%d] 高[%d]\n", stGlbMainWndHandle.s32ScreenWidth, stGlbMainWndHandle.s32ScreenHeight);


	return 0;
}


static void * ARTANIS_WINDOW_Proc(void *pArg)
{


	WND_HANDLE_CreateMainWnd(MAIN_WND_TYPE_MAIN_E);

	return NULL;
}


static int ARTANIS_WINDOW_Deinit(void *pArg)
{
	WND_HANDLE_DestroyAll();

	return 0;
}


static FUNC_MODULE_S stFModuleSet[] = {
	{
		.s8Name = "Environment",
		.stFops = {
			.FMODULE_Init = ARTANIS_ENVIRONMENT_Init,
			.FMODULE_DeInit = ARTANIS_ENVIRONMENT_Deinit,
			.FMODULE_Pthread = ARTANIS_ENVIRONMENT_Proc,
		},
	},
	{
		.s8Name = "window",
		.stFops = {
			.FMODULE_Init = ARTANIS_WINDOW_Init,
			.FMODULE_DeInit = ARTANIS_WINDOW_Deinit,
			.FMODULE_Pthread = ARTANIS_WINDOW_Proc,
		},
	},



};


static int ARTANIS_SYS_ShutDown(void)
{
	return 0;
}

static int ARTANIS_SYS_Init(void)
{
	return 0;
}


static int ARTANIS_SYS_Proc(void)
{
	return 0;
}


SYS_MODULE_S stGlbArtanis = {
	//.s8Manifesto = "Product[Artanis]\n",
	.pstFuncModuleSet = stFModuleSet,
	.s32FuncModuleNum = sizeof(stFModuleSet)/sizeof(FUNC_MODULE_S),
	.stFops = {
		.SYS_Init = ARTANIS_SYS_Init,
		.SYS_ShutDown = ARTANIS_SYS_ShutDown,
		.SYS_Proc = ARTANIS_SYS_Proc,
	},

};



#endif

